﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace Workshop
{
    public partial class FormAddIT : Form
    {

        public FormAddIT()
        {
            InitializeComponent();
        }

        private Boolean close = false;
        private String courseinstructor;
        public static Boolean wait = false;
        private String ID;
        struct course
        {
            public String subject;
            public String student;
            public String inID;
            public String instru;
        }
        static public int i = 0;
        private int num;
        static course[] c = new course[100];
        public FormAddIT(String n, String id, int nu)
        {
            //name = n;
            this.courseinstructor = n;
            //students = s;
            //groups = g;
            this.ID = id;
            this.num = nu;
            InitializeComponent();
        }

        public Boolean Waitforfinish()
        {
            if ((txtName.Text == "") && (txtStudent.Text == ""))
            {
                return false;
            }
            else return true;
        }

        private void putintostruct()
        {

            c[i].subject = txtName.Text;
            c[i].inID = ID;
            c[i].student = txtStudent.Text;
            c[i].instru = courseinstructor;


        }

        public void putintoxml(int times)
        {
            for (int j = 1; j <= times; j++)
            {
                XmlDocument xml = new XmlDocument();
                xml.Load("Course.xml");         //load the Course.xml
                String path1 = "//Courses/CourseInfo[@Number]";     //find the attribute of course number
                XmlNode node1 = xml.SelectSingleNode(path1);
                int temp;
                int.TryParse(node1.Attributes[0].Value, out temp);      //change the type from string to integer
                temp++;         //add 1 course
                node1.Attributes[0].Value = temp.ToString();        //cover the old value
                xml.Save("Course.xml");

                //create a new element named Course in the element of CourseInfo
                XmlNode courseinfo = xml.SelectSingleNode("/Courses/CourseInfo");
                XmlElement course = xml.CreateElement("Course");
                courseinfo.AppendChild(course);



                //create a new element named CourseID in the element of Course 
                XmlElement courseID = xml.CreateElement("CourseID");
                courseID.Attributes.Append(CreateAttribute(courseID, "FirstTime", ""));
                courseID.Attributes.Append(CreateAttribute(courseID, "SecondTime", ""));
                courseID.Attributes.Append(CreateAttribute(courseID, "ThirdTime", ""));
                courseID.InnerText = getLastID().ToString();
                course.AppendChild(courseID);

                //create a new element named Subject in the element of Course
                XmlElement subject = xml.CreateElement("Subject");
                subject.InnerText = c[j].subject;
                course.AppendChild(subject);

                //create a new element named Category in the element of Course
                XmlElement category = xml.CreateElement("Category");
                category.InnerText = "GE Course";
                course.AppendChild(category);

                //create a new element named Student in the element of Course
                XmlElement student = xml.CreateElement("Student");
                student.InnerText = c[j].student;
                course.AppendChild(student);

                //create a new element named TAID in the element of Course
                XmlElement taID1 = xml.CreateElement("TAID1");
                taID1.Attributes.Append(CreateAttribute(taID1, "TAName", ""));
                taID1.InnerText = null;
                course.AppendChild(taID1);

                //create a new element named TAID in the element of Course
                XmlElement taID2 = xml.CreateElement("TAID2");
                taID2.Attributes.Append(CreateAttribute(taID2, "TAName", ""));
                taID2.InnerText = null;
                course.AppendChild(taID2);

                //create a new element named Instructor in the element of Course
                XmlElement instructor = xml.CreateElement("InstructorID");
                instructor.Attributes.Append(CreateAttribute(instructor, "InstructorName", c[i].instru));
                instructor.InnerText = c[j].inID;
                course.AppendChild(instructor);

                XmlElement label = xml.CreateElement("label");
                //instructor.Attributes.Append(CreateAttribute(instructor, "", courseinstructor));
                label.InnerText = "IT";
                course.AppendChild(label);

                xml.Save("Course.xml");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() != String.Empty && txtStudent.Text.Trim() != String.Empty)
            {
                if (!checkthecourse(txtName.Text, txtStudent.Text) && !checkcourse(txtName.Text, txtStudent.Text))
                {
                    i++;
                    putintostruct();

                    if (i == num)
                    {
                        //MessageBox.Show("enter");
                        FormITgroups.counter = i;

                        //putintoxml(i);
                    }
                    wait = true;
                    //save the xml file
                    //xml.Save("Course.xml");
                    close = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("This course and student group has already assigned! Please try another course then save.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtName.Text = "";
                    txtStudent.Text = "";
                }
            }
            else
            {
                MessageBox.Show("Please input the information!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void FormAddIT_Load(object sender, EventArgs e)
        {
            //txtName.Text = name;
            txtInstructor.Text = courseinstructor;
            //txtStudent.Text = students;
        }

        private int getLastID()
        {
            int id = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            XmlNodeList nodelist = doc.SelectNodes("//Courses/CourseInfo/Course");
            for (int i = 0; i < nodelist.Count; i++)
            {
                if (i == nodelist.Count - 1)
                {
                    int.TryParse(nodelist[i].ChildNodes[0].InnerText, out id);

                }
            }



            //int.TryParse(nodelist[nodelist.Count].ChildNodes[0].InnerText, out id);
            id++;
            return id;

        }

        public static XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
        {
            try
            {
                XmlDocument doc = node.OwnerDocument;
                XmlAttribute attr = null;
                attr = doc.CreateAttribute(attributeName);
                attr.Value = value;
                node.Attributes.SetNamedItem(attr);
                return attr;
            }
            catch (Exception err)
            {
                string desc = err.Message;
                return null;
            }
        }

        private Boolean checkthecourse(String name, String student)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            XmlNodeList nodelist = doc.SelectNodes("//Courses/CourseInfo/Course");
            //if (nodelist != null)
            //{
            for (int i = 0; i < nodelist.Count; i++)
            {
                if (nodelist[i].ChildNodes[1].InnerText == name || nodelist[i].ChildNodes[3].InnerText == student) return true;
                //return true;
            }
            //}
            return false;

        }

        private Boolean checkcourse(String name, String student)
        {
            for (int k = 1; k <= i; k++)
            {
                if (c[k].subject == name || c[k].student == student) return true;
            }
            return false;
        }
        private void FormAddIT_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((e.CloseReason == CloseReason.UserClosing && txtName.Text.Trim() == String.Empty && txtStudent.Text.Trim() == String.Empty) || close == true)
            {
                /*DialogResult r = MessageBox.Show("确定要退出程序?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (r != DialogResult.OK)
                {
                    e.Cancel = true;
                }*/
                e.Cancel = false;
            }
            else
            {
                MessageBox.Show("You have input some informaiton, please save them first!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                e.Cancel = true;
            }
        }
    }
}
